#include "Button.h"
NAMESPACE_HEAD(mk)
NAMESPACE_HEAD(controls)

Button::Button(int px, int py, int pwidth, int pheight, std::string ptext): 
				Control(px,  py, pwidth, pheight)
{
	setDefault();
	m_type = ButtonType;
}
void Button::setDefault()
{
	currentState = BtnStateNormal;
	lock = false;
	func = NULL;
	texunit = NULL;
	passstateTexunit = NULL;
	pressedstateTexunit = NULL;
}
//////////////////////////////////////////////////////////////////////////
void Button::SetBtnState(ButtonState newState)
{
	currentState = newState;
}
void Button::RegistCallback(void(*callbackfunc)())
{
	func = callbackfunc;
}
void Button::On_Clicked()
{
	if(func != NULL)
	{
		if(lock == false)
		{
			lock = true;
			func();
			lock = false;
		}
		
	}
}

void Button::Draw()
{
	glEnable(GL_BLEND);
	switch(currentState)
	{
	case BtnStateNormal:	
		if(texunit)
			glBindTexture(GL_TEXTURE_2D,texunit->id);
		//std::cout<<name<<"  "<<texunit->id<<std::endl;
		break;
	case BtnStatePressed: 
		if(pressedstateTexunit)
			glBindTexture(GL_TEXTURE_2D,pressedstateTexunit->id); 
		break;
	case BtnStatePass: 
		if(passstateTexunit)
			glBindTexture(GL_TEXTURE_2D,passstateTexunit->id); 
		//std::cout<<name<<"  "<<passstateTexunit->id<<std::endl;
		break;
	default: 
		std::cout<<"sth error"<<std::endl;
	}
	glEnable(GL_TEXTURE_2D);
	glPushMatrix();
	glTranslatef(0,0,1);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0);
	glVertex2f(x,y);
	glTexCoord2f(1,0);
	glVertex2f(x + width, y);
	glTexCoord2f(1,1);
	glVertex2f(x +width, y + height);
	glTexCoord2f(0,1);
	glVertex2f(x, y + height);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
}
NAMESPACE_END()
NAMESPACE_END()
